package com.sakura.eduservice.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sakura.eduservice.client.VodClient;
import com.sakura.eduservice.entity.EduVideo;
import com.sakura.eduservice.mapper.EduVideoMapper;
import com.sakura.eduservice.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-03-02
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodClient vodClient;

    @Override
    public void removeVideoByCourseId(String courseId) {
        //1 根据课程id查询查询视频id
        List<EduVideo> eduVideoList = baseMapper.selectList(new QueryWrapper<EduVideo>()
                .eq("course_id", courseId).select("video_source_id"));

        //将List<EduVideo>转换为List<String>
        ArrayList<String> arrayList = new ArrayList<>();
        for (EduVideo video:eduVideoList){
            String sourceId = video.getVideoSourceId();
            if (!StringUtils.isEmpty(sourceId)){
                arrayList.add(video.getVideoSourceId());
            }
        }

        if (arrayList.size()>0){
            vodClient.deleteBatch(arrayList);
        }

        baseMapper.delete(new QueryWrapper<EduVideo>().eq("course_id",courseId));
    }
}
